// pages/search/search.js
// var app= getApp();//引用app.js
var app = getApp();
var commonURLYY = app.data.newURL;
var GZHid=53;//公众号id
var openid = app.data.openid;
var page = 1;
var pagesize = 12;
Page({ 
 /** 
  * 页面的初始数据 
  */
 data: { 
		menuList: [
	 	// {id: '01',menutype: "服饰"},
	 	// {id: '02',menutype: "男装"},
	 	// {id: '03',menutype: "母婴"},
	 	// {id: '04',menutype: "家居"},
	 	// {id: '05',menutype: "美食"},
	 	// {id: '06',menutype: "家纺"},
	 	// {id: '07',menutype: "美妆"},
	 	// {id: '08',menutype: "水果"},
	 	// {id: '09',menutype: "海淘"},
	 	// {id: '10',menutype: "服务"},
	 	// {id: '11',menutype: "热门"}
	],
	productList: [  					//这个其实是二级分类
	], 
	goodsList: [
	],
	input_value:[   
		// 搜索记录
	],
	hot_search: [
		// { id: '0301',name: '袜子', image: ''},
		// { id: '0302',name: '电饭煲', image: ''},
		// { id: '0303',name: '手机', image: ''},
		// { id: '0304',name: '键盘', image: ''},
		// { id: '0305',name: '沙发', image: ''},
		// { id: '0306',name: '家居', image: ''}
	],
	value: '', //input输入内容
  	toView: 'inToView01',
 }, 
	 onLoad: function (option) {
	 	page = 1;   						//page初始化
	 	var that = this;
	 	wx.request({         				//菜单
	 		url: commonURLYY+'op=fenlei',
	 		header: {
	        	'content-type': 'application/json'
	      	},
	      	success: function (res) {
	      		var first_menu = res.data.menuList[0];
	      		that.setData({
	      			menuList: res.data.menuList,
	      			type_name: first_menu.name,
	      			type_id: first_menu.id
	      		})
	      		that.getfenleiTwo(first_menu.id);
	      		that.getproductList(first_menu.id,page,pagesize);
	      	}
	 	})
	 	wx.request({						//热搜
	 		url: commonURLYY+'op=hot_search_view',
	 		header: {
	 			'content-type': 'application/json'
	 		},
	 		success: function (res) {
	 			that.setData({
	 				hot_search: res.data.list
	 			})
	 		} 
	 	})
	 	var postCollected_List = wx.getStorageSync('input_value');
	 	if(postCollected_List) {
	 		console.log(postCollected_List);
	 		this.setData({
	 			input_value: postCollected_List,
	 			show_history_search: true
	 		})
	 	}
	 },
	onReady: function () {
		
	},
	onShow: function () {
		this.onclear();
	},
	onReachBottom: function () {
		var that = this;
		page++;
		wx.request({
			url: commonURLYY + 'op=ajax',
			data: {
				cid: this.data.type_id,
				page: page,
				pagesize: pagesize
			},
			header: {
				'content-type': 'application/json'
			},
			success: function (res) {
				if(res.data.list.length < 1) {
					wx.showToast({
						title: '全部商品加载完成',
						icon: 'success',
						duration: 1000
					})
					return false;
				}
				wx.showToast({
					title: '商品加载中...',
					icon: 'loading',
					duration: 700
				})
				var i = 0,lists = [],length = res.data.list.length;
				for(;i < length; i++ ) {
					var obj = {};
					var indexid = obj["id"] = res.data.list[i].id;
					obj['gimg'] = res.data.list[i].gimg;
					obj['gname'] = res.data.list[i].gname;
					obj["price"]=res.data.list[i].oprice;//单买价
					obj["gprice"]=res.data.list[i].gprice;//团购价
					obj["peoplenum"]=res.data.list[i].groupnum;
					obj["unit"]=res.data.list[i].unit;
		            obj["mprice"]=res.data.list[i].mprice;//市场价
		            obj["salenum"]=res.data.list[i].salenum;
		            obj["xiangqing"]=res.data.list[i].gdesc;
		            obj["shuxing"]=res.data.list[i].params;
		            obj["advs"]=res.data.list[i].advs;
		            obj["kucun"]=res.data.list[i].gnum;
		            obj["selltype"]=res.data.list[i].selltype;
		            obj["hasoption"]=res.data.list[i].hasoption;//是否有规格 0没有 ，1有
		            obj["weight"]=res.data.list[i].weight;//重量，用于计算运费
		            obj["deliverytype"]=res.data.list[i].deliverytype;//配送方式
					if(res.data.list[i].gnum=="0"){
		              obj["smtuan"]="售罄";
		              obj["backg"]="#aaa";
		              obj["url"]='';
		            }else{
		              obj["smtuan"]="随意团  >";
		              obj["url"]='../index_detail/index_detail?id='+indexid;
		            }
		            lists.push(obj);
            		// dataLists[indexid]=obj;
				}
				that.setData({
		            goodsList:that.data.goodsList.concat(lists)
		        });
		        // wx.getStorage({
		        // 	key: 'goodsLists',
		        // 	success: function (res) {
		        // 		var data = res.data[indexid];
		        // 		if(!res.data[indexid]) { //如果缓存中不存在对应的商品，就ajax加载数据
		        // 			wx.setStorage({
			       //            key:"goodsLists",
			       //            data:dataLists
			       //          })
		        // 		} 
		        // 	} 
		        // })
			}
		})
	},
	getfenleiTwo: function (id) {
		var that = this;
		wx.request({ 						//分类小图
			url: commonURLYY+'op=fenleiTwo',
			data: {
				cid: id
			},
			header: {
	        'content-type': 'application/json'
	      	},
			success: function (res) {
				that.setData({
					productList : res.data.productList
				})
			}
		})
	},
	getproductList: function (id,page,pagesize) {
		var that = this;
		wx.request({   						//商品列表
			url: commonURLYY+'op=ajax',
			data: {
				cid: id,
				page: page,
				pagesize: pagesize	
			},
			header: {
				'content-type': 'application/json'
			},
			success: function (res) {
				that.setData({
					goodsList: res.data.list
				})
			}
		})
	},
 	scrollToViewFn: function (e) {
 		page = 1;  								//初始化page,在选择新分类的时候 
		var _id = e.currentTarget.dataset.id;
		var _name = e.currentTarget.dataset.name;
		this.data.productList = []; 
		this.setData({
			type_name: _name,
			type_id: _id
		})
		this.getfenleiTwo(_id);
		this.getproductList(_id,page,pagesize);
	},
	onfenleiTwo: function (e) {   //进入分类
		var _child = e.currentTarget.dataset.id;
		var _name = e.currentTarget.dataset.name;
		var _id = e.currentTarget.dataset.parent;
		wx.navigateTo({
			url: '../navbar-detail/navbar-detail?id='+ _id+'&name='+_name+'&child='+_child
		})
	}, 
	onshowsearch: function () {  //显示搜索页面
		this.setData({
			showsearch: true   
		})
	},
	onclose_search: function () {
		this.setData({
			showsearch: false
		})
	},
	oninput_change: function (event){ //显示搜索字样
		var that = this;
		this.setData({
			value: event.detail.value,
			search_text: true
		})
		if(event.detail.value == ''){
			this.setData({
				search_text: false
			})
		}
	},
	// getsearch_result: function (key) {
	// 	wx.request({
	// 		url: commonURLYY+'op=search_goods_name',
	// 		data: {
	// 			key: key
	// 		},
	// 		header: {
	// 			'content-type': 'application/json'
	// 		},
	// 		success: function (res) {
	// 			that.setData({
	// 				mohu_search: res.data.data
	// 			})
	// 		}
	// 	})
	// },
	onget_value: function (event) {  //input  bindconfirm  输入完成 触发
		console.log(event.detail.value);
		var detailValue_arr = this.data.input_value;
		if(!event.detail.value == ''){  //空值不添加
			detailValue_arr.push(event.detail.value);
		}
		detailValue_arr = Array.from(new Set(detailValue_arr));  //数组去重
		this.setData({
			input_value: detailValue_arr,
			show_history_search: true
		})

		//这里按下键盘回车,缓存,但是点击搜索并没有,触发onget_value事件
		wx.setStorageSync('input_value',this.data.input_value);  //实测storage可以存数组,读取也没有问题
		console.log(this.data.input_value);
	},
	onsearch: function () { 
		var detailValue_arr = this.data.input_value;
		if(!this.data.value == ''){  //空值不添加
			detailValue_arr.push(this.data.value);  //添加当前出入框的内容
		}
		detailValue_arr = Array.from(new Set(detailValue_arr));  //数组去重
		this.setData({
			input_value: detailValue_arr,
			show_history_search: true
		})
		wx.setStorageSync('input_value',this.data.input_value);  //实测storage可以存数组,读取也没有问题
		wx.navigateTo({
			url: 'search-result/search-result?value='+this.data.value 
		})
	},
	ondelete: function () {
		this.setData({
			input_value: [],  //注意,要先将原先的在data里面的数组清空,不然,下次添加的时候会在原先的数组里面添加,就做不到清空缓存的效果;
			show_history_search: false
		})
		wx.removeStorage({   //这种方法造成的现象跟诡异
			key:'input_value',
			success:function () {
				console.log('成功清除缓存!')
			}
		})
		wx.showToast({
            title: "清除成功",
            duration: 1000,
            icon: "success"
        })
	},
	onclear: function () {
		this.setData({
			value: '',
			search_text: false
		})
	},
	search_history: function (e) {
		var value = e.currentTarget.dataset.value;
		this.setData({
			value: value
		})
		this.onsearch();
	},
	go_detail: function (e) {    //去商品详情页
		var _id = e.currentTarget.dataset.id;
		var _selltype = e.currentTarget.dataset.selltype;
		if(_selltype == '0'){
			wx.navigateTo({
				url: '../index_only_detail/index_only_detail?id='+_id
			})
		}else{
			wx.navigateTo({
				url: '../index_detail/index_detail?id='+_id
			})
		}
	}
}) 
